﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using reminder_csh.Entities;
using reminder_csh.IManager;

namespace reminder_csh {
    class AssignmentManagerImpl : IAssignmentManager, IDisposable {
        private MyDbContext db=new MyDbContext();

        public void AssignTaskToWorker(Task task, Worker worker) {
            var wId=worker.Id;
            var w=db.WorkerDbSet.Find(wId);

            var t=db.TaskDbSet.Find(task.Id);

            w.Tasks.Add(t);
            db.WorkerDbSet.Attach(w);

            db.SaveChanges();
        }

        public void RemoveTaskFromWorker(Task task, Worker worker) {
            var wId=worker.Id;
            var w=db.WorkerDbSet.Find(wId);
            w.Tasks.Remove(task);
            db.Entry(w).State=EntityState.Modified;

            db.SaveChanges();
        }

        public List<Task> FindTasksOfWorker(Worker worker) {
            var wId=worker.Id;
            var w=db.WorkerDbSet.Find(wId);
            return w.Tasks.ToList();
        }

        public List<Worker> FindWorkersOfTask(Task task) {
            var tId=task.Id;
            var t=db.TaskDbSet.Find(tId);
            return t.Workers.ToList();
        }

        public void Dispose() {
            db.Dispose();
        }
    }
}
